package com.hongdee.insurance.company.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.base.imp.vo.ExcelSchemeVo;
import com.hongdee.common.util.StringUtil;
import com.hongdee.insurance.company.entity.TInsuranceCompanyBill;
import com.hongdee.insurance.company.service.ITInsuranceCompanyBillService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 王帅
 * @className TInsuranceCompanyBillController
 * @type class
 * @description
 * @path com.hongdee.insurance.company.controller
 * @create 2017/6/14 8:46
 **/
@Controller
@RequestMapping("/insurance/company/bill")
public class TInsuranceCompanyBillController {


    @Autowired
    private ITInsuranceCompanyBillService billService;

    /**
     * 新增对账单
     * @param uid
     * @param token
     * @param data
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TInsuranceCompanyBill data) throws Exception {
        ResultEntity res=new ResultEntity();
        data=billService.save(data,uid);
        res.setData(data);
        res.setSuccess(true);
        res.setMsg("录入成功！！");
        return res;
    }

    /**
     * 获取对账单详情
     * @param uid
     * @param token
     * @param billId
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "billId", defaultValue = "") String billId) throws Exception {
        ResultEntity res=new ResultEntity();
        TInsuranceCompanyBill bill=this.billService.getBillInfo(billId);
        res.setData(bill);
        res.setSuccess(true);
        res.setMsg("查询保单详情成功！！");
        return res;
    }

    /**
     * 分页查询对账单信息
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public ResultEntity page(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "start", defaultValue = "") String start,
                             @RequestParam(name = "limit", defaultValue = "") String limit,
                             @RequestBody Map data) throws Exception {
        ResultEntity res=new ResultEntity();
        Map map=this.billService.page(data,uid,start,limit);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询成功！！");
        return res;
    }

    /**
     * 根据对账单信息显示明细
     * @param uid
     * @param token
     * @param billId
     * @return
     */
    @RequestMapping("/billDetail")
    @ResponseBody
    public Map billDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "billId", defaultValue = "") String billId,
                                   @RequestParam(name = "start", defaultValue = "") String start,
                                   @RequestParam(name = "limit", defaultValue = "") String limit,
                                   @RequestBody Map data) throws Exception {
        Map map=billService.getBillDetailList(billId,start,limit,data);
        map.put("success",true);
        map.put("msg","查询成功!!");
        return map;
    }

    /**
     * 根据对账单信息显示明细
     * @param uid
     * @param token
     * @param billId
     * @return
     */
    @RequestMapping("/billDetail/exp/excel")
    @ResponseBody
    public ResultEntity billDetailExpExcel(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "billId", defaultValue = "") String billId,
                                  HttpServletRequest request, HttpServletResponse response,
                                  @RequestBody Map data) throws Exception {
        XSSFWorkbook wb = billService.billDetailExpExcel(uid,billId,data);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
        path =   path + File.separator + "upload";

        File filepack = new File(path);
        if(!filepack.exists()){
            filepack.mkdir();
        }
        String filePath =   path + File.separator + fileId + ".xlsx";
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }

    /**
     * 清空对账单明细
     * @param uid
     * @param token
     * @param billId
     * @return
     * @throws Exception
     */
    @RequestMapping("/moveBillDetail")
    @ResponseBody
    public ResultEntity moveBillDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                       @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestParam(name = "billId", defaultValue = "") String billId) throws Exception {
        ResultEntity res = new ResultEntity();
        billService.moveBillDetail(billId);
        res.setSuccess(true);
        res.setMsg("清空对账明细成功，请重新上传！！");
        return res;
    }

    /**
     * 删除对账单信息成功
     * @param uid
     * @param token
     * @param billId
     * @return
     * @throws Exception
     */
    @RequestMapping("/del")
    @ResponseBody
    public ResultEntity del(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "billId", defaultValue = "") String billId) throws Exception {
        ResultEntity res = new ResultEntity();
        billService.delBill(billId);
        res.setSuccess(true);
        res.setMsg("删除对账单信息成功！！");
        return res;
    }

    /**
     * 保险对账单结算
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/balance")
    @ResponseBody
    public ResultEntity exam(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "billId", defaultValue = "") String billId,
                             @RequestBody Map data) throws Exception {
        ResultEntity res=new ResultEntity();
        this.billService.balanceOrder(data,uid,billId);
        res.setSuccess(true);
        res.setMsg("结算成功！！");
        return res;
    }

    /**
     * 导入excel模板
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/excelCombo")
    @ResponseBody
    public ResultEntity excelCombo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        ResultEntity res=new ResultEntity();
        res.setSuccess(true);
        List<Map<String,Object>> list=this.billService.findExcelScheme();
        res.setData(list);
        return res;
    }


    /**
     * 上传 Execl
     *
     * @param uid
     * @param token
     * @param schemeId
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    @ResponseBody
    public ResultEntity excelImport(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "schemeId", defaultValue = "") String schemeId,
                                    @RequestParam(name = "billId", defaultValue = "") String billId,
                                    @RequestParam(name = "upExecl", defaultValue = "upExecl") MultipartFile file) throws Exception {

        if (StringUtils.isEmpty(schemeId)) {
            throw new Exception("请选择方案");
        }
        if(StringUtil.formatEmpty(billId).equals("")){
            throw new Exception("请选择对账单！！");
        }
        final ExcelSchemeVo scheme = this.billService.loadExcelSchemeInfo(schemeId);

        int[] successNum = billService.parseExcel(file, scheme, uid,billId);

        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setTotal(0L + successNum[0]);
        res.setMsg("导入成功" + successNum[0] + "行");
        return res;
    }


    /**
     * 开票
     * @param uid
     * @param token
     * @param billId
     * @return
     */
    @RequestMapping("/billing")
    @ResponseBody
    public ResultEntity billing(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "billId", defaultValue = "") String billId,
                                @RequestParam(name = "invoiceNumber",defaultValue = "")String invoiceNumber,
                                @RequestParam(name = "billingPrice",defaultValue = "")String billingPrice,
                                @RequestParam(name = "billingDate", defaultValue = "") String billingDate) throws Exception {
        if("".equals(invoiceNumber)){
            throw new Exception("请输入发票号！！");
        }
        ResultEntity res=new ResultEntity();
        billService.billing(billId,billingDate,billingPrice,invoiceNumber);
        res.setSuccess(true);
        res.setMsg("开票信息录入成功！！");
        return res;
    }


    /**
     * 上传  Execl 用于结平对账单
     *
     * @param uid
     * @param token
     * @param schemeId
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/balance/upload")
    @ResponseBody
    public ResultEntity balanceupload(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "schemeId", defaultValue = "") String schemeId,
                                    @RequestParam(name = "billId", defaultValue = "") String billId,
                                    @RequestParam(name = "upExecl", defaultValue = "upExecl") MultipartFile file) throws Exception {

        if (StringUtils.isEmpty(schemeId)) {
            throw new Exception("请选择方案");
        }
        if(StringUtil.formatEmpty(billId).equals("")){
            throw new Exception("请选择对账单！！");
        }
        //解析
        final ExcelSchemeVo scheme = this.billService.loadExcelSchemeInfo(schemeId);
        //导入
        String type="bd";
        billService.parseExcelByBalance(file, scheme, uid,billId,type);

        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("本次对账完成!!");
        return res;
    }

    /**
     * 发票信息平账
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/balance/list")
    @ResponseBody
    public ResultEntity balanceList(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token,
                                      @RequestParam(name = "invoiceNum", defaultValue = "")String invoiceNum,
                                      @RequestParam(name = "invoiceDate",defaultValue = "")String invoiceDate,
                                      @RequestParam(name = "invoicePrice", defaultValue = "")String invoicePrice,
                                      @RequestBody List<Map> data) throws Exception {

        if(data.size()==0){
            throw new Exception("请选择对账单！！");
        }
        //导入
        String type="fp";
        billService.diffBalanceBillByDateList(data,invoiceNum,invoiceDate,invoicePrice,uid,type);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("本次对账完成!!");
        return res;
    }

    /**
     * 调整日志
     * @param uid
     * @param token
     * @param billId
     * @return
     */
    @RequestMapping("/balance/adjust/log")
    @ResponseBody
    public Map adjustLog(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "billId", defaultValue = "") String billId,
                                  @RequestParam(name = "start", defaultValue = "") String start,
                                  @RequestParam(name = "limit", defaultValue = "") String limit) throws Exception {
        if("".equals(billId)){
            throw new Exception("请选择对账单!!");
        }
        Map map=this.billService.findBalanceAdjustLogs(billId,start,limit);
        map.put("success",true);
        map.put("msg","查询成功!!");
        return map;
    }



}
